package com.ruipengkj.view.workShiftView;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TOrder;
import com.ruipengkj.bean.TOrderSummary;
import com.ruipengkj.bean.TShopCharge;
import com.ruipengkj.bean.TWorkShift;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.service.TOrderService;
import com.ruipengkj.service.TOrderSummaryService;
import com.ruipengkj.service.TShopChargeService;
import com.ruipengkj.service.TWorkShiftService;
import com.ruipengkj.view.base.BasePanel;

public class WorkShiftPanel extends BasePanel implements Runnable{

	private static final long serialVersionUID = 1L;
//	private JLabel label1;
//	private JLabel label2;
//	private JLabel label3;
	private JLabel label4;
	private JLabel label5;
	private JLabel label6;
	private JLabel label7;
//	private JLabel label8;
	private JLabel label9;
	private JLabel label10;
//	private JLabel label11;
//	private JLabel label12;
	private JLabel label13;
	private JLabel label14;
//	private JLabel label15;
//	private JLabel label16;
//	private JLabel label17;
//	private JLabel label18;
//	private JLabel label19;
	private JLabel label20;
	
//	private JLabel UpAmount;//前班总金额
//	private JLabel ToAmount;//交班金额
//	private JLabel UpEmployee;//前班员工
	private JLabel ToEmployee;//本班员工
	private JLabel HappenTime;//交班时间
	private JLabel STotalIncome;//系统记录总收入
	private JLabel SOrderCount;//系统记录总消费次数
//	private JLabel SOrderAmount;//系统记录订单收入
	private JLabel SOrderCardAmount;//系统记录订单会员卡消费总金额
	private JLabel SOrderDiscountAmount;//系统记录订单优惠券金额
//	private JLabel SOrderDebtAmount;//系统记录订单顾客欠款
//	private JLabel SOrderFreeAmount;//系统记录订单免单金额
	private JLabel SCardRechangeAmount;//系统记录会员卡充值金额
	private JLabel SOtherCashAmount;//系统记录其他现金收入
//	private JLabel SOtherOutCachAmount;//系统记录其他现金支出
//	private JLabel STotalAmount;//系统记录合计总金额
//	private JLabel SOrderAccountAmount;//系统记录订单各账号总金额
//	private JLabel STotalInAmount;//系统记录记录合计收入
//	private JLabel STotalOutAmount;//系统记录合计总支出
	private JLabel STotalCashAmount;//系统记录现金总收入
	
	private double sTotalIncome = 0;
	private int sOrderCount = 0;
	private double sOrderAmount = 0;
	private double sOrderCardAmount = 0;
	private double sOrderDiscountAmount = 0;
	private double sOrderDebtAmount = 0;
	private double sOrderFreeAmount = 0;
	private double sCardRechangeAmount = 0;
	private double sOtherCashAmount = 0;
	private double sOtherOutCachAmount = 0;
	private double sTotalAmount = 0;
	private double sOrderAccountAmount = 0;
	private double sTotalInAmount = 0;
	private double sTotalOutAmount = 0;
	private double sTotalCashAmount = 0;
	
	private JButton sureButton = null; //确定
	private JButton cancelButton = null; //取消

	private TWorkShiftService tWorkShiftService;
//	private TWorkShift tWorkShift = new TWorkShift();
//	private List<TWorkShift> tWorkShiftList = new ArrayList();
	private TOrderService tOrderService;
	private TOrder tOrder = new TOrder();
//	private List<TOrder> tOrderList = new ArrayList<TOrder>();
	private TShopChargeService tShopChargeService;
	private TShopCharge tShopCharge = new TShopCharge();
//	private List<TShopCharge> tShopChargeList = new ArrayList<TShopCharge>(); 
	private TOrderSummaryService tOrderSummaryService;
	private TOrderSummary tOrderSummary = new TOrderSummary();
	private List<TOrderSummary> tOrderSummaryList = new ArrayList<TOrderSummary>();
	
	Date date = new Date();
	
	public WorkShiftPanel(){
		tWorkShiftService = BuildSpringApplication.getInstance().getTWorkShiftServiceImpl();
		tOrderService = BuildSpringApplication.getInstance().getTOrderServiceImpl();
		tShopChargeService = BuildSpringApplication.getInstance().getTShopChargeServiceImpl();
		tOrderSummaryService = BuildSpringApplication.getInstance().getTOrderSummaryServiceImpl();
		this.getInfo();
		this.addComponents();
		this.initListener();
	}
	
	private void getInfo(){
		tOrderSummaryList = tOrderSummaryService.findByWorkShiftID(ApplicationContext.WORKSHIFT_ID);
		tOrderSummary = tOrderSummaryList.get(0);
		
		sOrderCount=tOrderSummary.getCounts();
		sOrderCardAmount=tOrderSummary.getCardAmounts();
		sOrderDiscountAmount=tOrderSummary.getDiscountAmounts();
		sOtherCashAmount=tOrderSummary.getCashAmounts();
		sCardRechangeAmount=tOrderSummary.getRechangeAmounts();
		
		sTotalCashAmount=sOtherCashAmount+sCardRechangeAmount;
		sTotalIncome=sOrderCardAmount+sOrderDiscountAmount+sOtherCashAmount+sCardRechangeAmount;
		
//		TWorkShift shift = new TWorkShift();
//		tWorkShift = tWorkShiftService.findLastOne();
		
//		try {
//			tOrder = new TOrder();
//			tOrder.setWorkShiftID(ApplicationContext.WORKSHIFT_ID);
//			tOrderList = tOrderService.findByWorkShiftID(tOrder);
//			tShopCharge = new TShopCharge();
//			tShopCharge.setWorkShiftID(ApplicationContext.WORKSHIFT_ID);
//			tShopChargeList = tShopChargeService.findByWorkShiftID(tShopCharge);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		for(int i = 0; i < tShopChargeList.size();i++){
//			tShopCharge = tShopChargeList.get(i);
//			sCardRechangeAmount += tShopCharge.getAMount();
//		}
//		for(int i = 0; i < tOrderList.size();i++){
//			tOrder = tOrderList.get(i);
//			sOrderCount = tOrderList.size();
//			if(tOrder.getPayWay()==8){
//				sOrderCardAmount += tOrder.getCardPayedAmount();
//			}
//			sOrderCachCardAmount = tOrder.getCashCardPayedAmount();
//			if(tOrder.getPayWay()==7){
//				System.out.print(tOrder.getCachPayedAmount());
//				sOtherCashAmount += tOrder.getCachPayedAmount();
//			}
//		}
//		sTotalIncome = sCardRechangeAmount + sOrderCardAmount + sOrderCachCardAmount + sOtherCashAmount;
	}

	private void addComponents(){
		JPanel main_jp=new JPanel();
		main_jp.setBorder(BorderFactory.createTitledBorder("交班信息"));
//		BoxLayout boxLayout=new BoxLayout(main_jp, BoxLayout.Y_AXIS);
		GridLayout gridLayout = new GridLayout(10,2);
		main_jp.setLayout(gridLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		
//		FlowLayout flowLayout=new FlowLayout(FlowLayout.LEFT);
//		JPanel workShift_jp=new JPanel(flowLayout);
		
//		label1 = new JLabel("前班总金额:");
//		UpAmount = new JLabel(String.valueOf(tWorkShift.getToAmount()));
//		label2 = new JLabel("交班金额:");
//		ToAmount = new JLabel(String.valueOf(sOrderAmount));
//		label3 = new JLabel("前班员工");
//		UpEmployee = new JLabel(tWorkShift.getUpEmployeeID());
		label4 = new JLabel("本班员工:");
		ToEmployee = new JLabel();
		label5 = new JLabel("交班时间:");
		HappenTime = new JLabel();
		label6 = new JLabel("系统记录总收入:");
		STotalIncome = new JLabel();
//		STotalIncome = new JLabel("aaaaaaaaaaaaa");
		label7 = new JLabel("系统记录总消费次数:");
		SOrderCount = new JLabel();
		label9 = new JLabel("系统记录订单会员卡消费总金额:");
		SOrderCardAmount = new JLabel();
		label10 = new JLabel("系统记录订单优惠券金额:");
		SOrderDiscountAmount = new JLabel();
		label13 = new JLabel("系统记录会员卡充值金额:");
		SCardRechangeAmount = new JLabel();
		label14 = new JLabel("系统记录其他现金收入:");
		SOtherCashAmount = new JLabel();
		label20 = new JLabel("系统记录现金总收入:");
		STotalCashAmount = new JLabel();
		
		JPanel jp6 = uiFactory.createFlowJPanel(FlowLayout.LEFT);
		sureButton = new RPJButtonBlue("确定(Ctrl+Y)");
		cancelButton = new RPJButtonBlue("取消");
		jp6.add(sureButton);
		jp6.add(cancelButton);
//		main_jp.add(label1);
//		main_jp.add(UpAmount);
//		main_jp.add(label2);
//		main_jp.add(ToAmount);
//		main_jp.add(label3);
//		main_jp.add(UpEmployee);
		main_jp.add(label4);
		main_jp.add(ToEmployee);
		main_jp.add(label5);
		main_jp.add(HappenTime);
		main_jp.add(label6);
		main_jp.add(STotalIncome);
		main_jp.add(label7);
		main_jp.add(SOrderCount);
		main_jp.add(label9);
		main_jp.add(SOrderCardAmount);
		main_jp.add(label10);
		main_jp.add(SOrderDiscountAmount);
		main_jp.add(label13);
		main_jp.add(SCardRechangeAmount);
		main_jp.add(label14);
		main_jp.add(SOtherCashAmount);
		main_jp.add(label20);
		main_jp.add(STotalCashAmount);
		main_jp.add(jp6);
		this.add(main_jp);
	}
	
	private void initListener() {
		sureButton.addActionListener(this);		
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==sureButton){
			
			TWorkShift tws = new TWorkShift();
			tws.setWorkShiftID(ApplicationContext.WORKSHIFT_ID);
//			tws.setUpAmount(tWorkShift.getToAmount());
//			tws.setToAmount(sOrderAmount);
//			tws.setUpEmployeeID(tWorkShift.getToEmployeeID());
			tws.setToEmployeeID(ApplicationContext.EMPLOYEE.getEmployeeID());
			tws.setHappenTime(date); 
			tws.setSTotalIncome(sTotalIncome);
			tws.setSOrderCount(sOrderCount);
			tws.setSOrderCardAmount(sOrderCardAmount);
			tws.setSOrderCachCardAmount(sOrderDiscountAmount);
			tws.setSCardRechangeAmount(sCardRechangeAmount);
			tws.setSOtherCashAmount(sOtherCashAmount);
			tws.setStatus(ApplicationContext.WORKSHIFT_STATUS2);
			try {
				tWorkShiftService.saveOrUpdateService(tws);
				BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("交班记录编号：+"+tws.getWorkShiftID());
				JOptionPane.showMessageDialog(null, "操作成功");
				this.clear();				
//				RPLogin login = new RPLogin();
//				login.main(args);
			} catch (Exception e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(null, "操作失败，请重新操作");
				this.clear();
			}
			
			
//			java.awt.EventQueue.invokeLater(new Runnable(){
//                public void run(){
//                    final JFrame frame = new JFrame("RPLogin");
//                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//                    frame.setPreferredSize(new Dimension(400,300));
//                    final JButton restart = new JButton("Restart");
//                    restart.addActionListener(new ActionListener(){
//                            @Override public void actionPerformed(ActionEvent e){
//                                Runtime.getRuntime().addShutdownHook(new Thread(){
//                                        @Override public void run(){
//                                        	RPLogin.main(new String[0]);
////                                            try {
////                                            	System.out
////														.println("XXXXXXXXXX");
////                                                new ProcessBuilder("java","RPLogin").start();
////                                            } catch (IOException e) {
////                                            }
//                                        }
//                                    });
//                                System.exit(0);
//                            }
//                        });
//                    frame.getContentPane().add(restart,"South");
//                    frame.pack();
//                    frame.setVisible(true);
//                }
//            });
			
//			
//			Runtime.getRuntime().addShutdownHook(new Thread() {
//	            public void run() {
//	                try {
//						Runtime.getRuntime().exec("calc");
//					} catch (IOException e) {
//						e.printStackTrace();
//					} 
////	            	System.out.println("XXXXXXXXXXX");
////	                RPLogin.main(new String[0]); //或者这样试试看
//	            }    
//	        });
			System.out.println("--------");
	        System.exit(0);
		}
	}
	public void clear(){
//		UpAmount.setText("");
//		ToAmount.setText("");
//		UpEmployee.setText("");
//		ToEmployee.setText("");
//		HappenTime.setText("");
//		STotalIncome.setText("");
//		SOrderCount.setText("");
//		SOrderCardAmount.setText("");
//		SOrderCachCardAmount.setText("");
//		SCardRechangeAmount.setText("");
//		SOtherCashAmount.setText("");
	}

	@Override
	public void run() {
		this.getInfo();
		ToEmployee.setText(ApplicationContext.EMPLOYEE.getName());
		SimpleDateFormat shijian= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		HappenTime.setText(shijian.format(date));
		STotalIncome.setText(String.valueOf(sTotalIncome));
		SOrderCount.setText(String.valueOf(sOrderCount));
		SOrderCardAmount.setText(String.valueOf(sOrderCardAmount));
		SOrderDiscountAmount.setText(String.valueOf(sOrderDiscountAmount));
		SCardRechangeAmount.setText(String.valueOf(sCardRechangeAmount));
		SOtherCashAmount.setText(String.valueOf(sOtherCashAmount));
		STotalCashAmount.setText(String.valueOf(sTotalCashAmount));
	}
}
